package com.hdrx.fundhelper.utils;


import cn.hutool.core.util.ObjectUtil;
import com.hdrx.fundhelper.common.ParamException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author: houdx
 * @description:
 * @date: 2024/1/15 9:06
 */
@Slf4j
public class ReadExcleUtil {

    private static final String FILE_SUFFIX_XLS = "xls";
    private static final String FILE_SUFFIX_XLSX = "xlsx";


    public static void readExcleUtil(String filePath, String sheetName) {

        checkStringIsNull("readExcleUtil", filePath, sheetName);

        FileInputStream fileInputStream ;

        String[] split = filePath.split("\\.");
        if (FILE_SUFFIX_XLS.equals(split[1])) {
            //HSSFWorkbook:是操作Excel2003以前（包括2003）的版本，扩展名是.xls；
            HSSFWorkbook workbook = null;
            try {
                fileInputStream = new FileInputStream(filePath);
                workbook = new HSSFWorkbook(fileInputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (ObjectUtil.isEmpty(workbook)){
                throw new ParamException("解析文件失败");
            }
            assert workbook != null;
            HSSFSheet sheet = workbook.getSheet(sheetName);
            //读行，计数从0开始
            HSSFRow row = sheet.getRow(3);
            //读列，计数从0开始
            HSSFCell cell = row.getCell(1);
            //return cell.toString();
        }
        if (FILE_SUFFIX_XLSX.equals(split[1])) {

            XSSFWorkbook workbook1 = null;
            try {
                fileInputStream = new FileInputStream(filePath);
                workbook1 = new XSSFWorkbook(fileInputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (ObjectUtil.isEmpty(workbook1)){
                throw new ParamException("解析文件失败");
            }
            assert workbook1 != null;
            XSSFSheet sheet = workbook1.getSheet(sheetName);
            //读行，计数从0开始
            XSSFRow row = sheet.getRow(3);
            //读列，计数从0开始
            XSSFCell cell = row.getCell(1);
            //return cell.toString();
        }
        //return null;
    }

    public static void main(String[] args) {
        readExcleUtil("D:\\test\\国债逆回购.xlsx", "Sheet1");
    }

    /*public static void main(String[] args) {
        File file = new File("D:\\test");
        File[] files = file.listFiles();
        for (File file1 : files) {
            String name = file1.getName();
            System.out.println(name);
        }
    }*/

    /**
     * 检查字符是否为 null || ""
     * 如果是 null 或者 "" 则返回false ,反则为 true
     *
     * @param methodName 调用方法名称 (哪个方法使用此验证)
     */
    public static void checkStringIsNull(String methodName, String filePath, String sheetName) {

        if (StringUtils.isBlank(filePath)) {
            log.info("方法[{}]的参数[filePath]为空", methodName);
            throw new ParamException("参数[filePath]为空");
        }
        if (StringUtils.isBlank(sheetName)) {
            log.info("方法[{}]的参数[sheetName]为空", methodName);
            throw new ParamException("参数[sheetName]为空");
        }

    }
}
